package util;

import java.util.ArrayList;
import java.util.Random;

public class codeutil {
    public static String getcode(){
        //创建集合存放大小写字母
        ArrayList<Character> list=new ArrayList<>();
        //往集合里添加元素
        for (int i = 0; i < 26; i++) {
            list.add((char)('a'+i));
            list.add((char)('A'+i));
        }
        //打印集合
        System.out.println(list);
        //随机生成4个字母
        String result="";
        Random r=new Random();
        for (int i = 0; i < 4; i++) {
            int index=r.nextInt(list.size());
            char str=list.get(index);
            result+=str;
        }
        //随机生成0~9数字
        int num=r.nextInt(10);
        result+=num;
        //把字符串变成字符串数组
        char[] chars = result.toCharArray();
        //随机生成0~3索引
        int index=r.nextInt(4);
        //与最后的数字交换
        char tmp=chars[index];
        chars[index]=chars[4];
        chars[4]=tmp;
        //把字符数组变回字符串
        String code=new String(chars);
        return code;

    }
}
